<?php
/**
 * User: itimphp
 * Date: 6/14/12
 * Time: 1:21 AM
 */
require_once '../bootstrap.php';
include_once '../includes/formvalidator.php';
include_once '../includes/utils.php';

// Must login
if (empty($_SESSION['admin_id'])) {
	header('Location: login.php');
}

$__page = "league";

$league_id = isset($_GET['id']) ? intval($_GET['id']) : 0;
$selected_league = $my_db->select_row('SELECT * FROM leagues WHERE id = ?', array($league_id));
$sports = $my_db->select_pairs('SELECT * FROM sports');

if (isset($_GET['id']) && empty($selected_league)) {
	echo 'There are no such league in our system!';
} else {
	$data = $selected_league;
	if (isset($_POST['Submit'])){
		$data = $_POST;
		// Validation
		$validations = array(
			'firstName' => array('type' => 'words', 'message' => 'First name invalid'),
			'lastName' => array('type' => 'words', 'message' => 'Last name invalid'),
			'orgName' => array('type' => 'alfanum', 'message' => 'Name of league/org must invalid '),
			'address' => array('type' => 'alfanum', 'message' => 'Address invalid'),
			'city' => array('type' => 'alfanum', 'message' => 'You must enter a valid address'),
			'zipcode' => array('type' => 'anything', 'message' => 'invalid zipcode'),
			'phone' => array('type' => 'phone', 'message' => 'invalid phone number'),
			'email' => array('type' => 'email', 'message' => 'You must enter a valid email address'),
			'url' => array('type' => 'url', 'message' => 'You must enter a valid link'),
			'ageTo' => array('type' => 'number', 'message' => 'You must enter a valid age'),
			'ageFrom' => array('type' => 'number', 'message' => 'You must enter a valid age'),
			'registrationStart' => array('type' => 'date', 'message' => 'You must enter a valid registration start date'),
			'registrationEnd' => array('type' => 'date', 'message' => 'You must enter a valid registration end date'),
			'seasonStart' => array('type' => 'date', 'message' => 'You must enter a valid season start date'),
			'seasonEnd' => array('type' => 'date', 'message' => 'You must enter a valid season end date'),
		);
		// Required array
		$required = array('firstName', 'lastName', 'orgName', 'address', 'city', 'zipcode', 'phone', 'email',
			'url', 'sport', 'ageTo', 'ageFrom', 'gender', 'eligibleZipcodes', 'registrationStart',
			'registrationEnd', 'seasonStart', 'seasonEnd');
		$sanatize = array();
		foreach ($validations as $item_name => $options) {
			switch ($options['type']) {
				case 'anything':
					array_push($sanatize, $item_name);
					break;
				case 'number':
					$sanatize[$item_name] = 'int';
					break;
				case 'price':
					$sanatize[$item_name] = 'float';
					break;
				case 'email':
					$sanatize[$item_name] = 'email';
					break;
				default:
					break;
			}
		}
		$validator = new FormValidator($validations, $required, $sanatize);

		if ($validator->validate($data)) {
			// Very important!!
			$data = $validator->sanatize($data);
			$data_table = array($data['firstName'], $data['lastName'], $data['orgName'], $data['address'], $data['city'], $data['zipcode'], $data['phone'], $data['email'], $data['url'], $data['sport'], $data['ageTo'], $data['ageFrom'], $data['gender'], $data['eligibleZipcodes'], date('Y-m-d H:i:s',strtotime($data['registrationStart'])), date('Y-m-d H:i:s',strtotime($data['registrationEnd'])), date('Y-m-d H:i:s',strtotime($data['seasonStart'])), date('Y-m-d H:i:s',strtotime($data['seasonEnd'])));
			if (!empty($selected_league)) {
				// ACTION = EDIT
				$data [] = $_GET['id'];
				$return_code = $my_db->execute('UPDATE leagues SET `firstName` = ?, `lastName` = ?, `orgName` =?, `address` = ?, `city` = ?, `zipcode` = ?, `phone` = ?, `email` = ?, `url` = ?, `sport` = ?, `ageTo` = ?, `ageFrom` = ?, `gender` = ?, `eligibleZipcodes` = ?, `registrationStart` = ?, `registrationEnd` = ?, `seasonStart` = ?, `seasonEnd`=? WHERE id = ?', $data_table);
			} else {
				// ACTION = ADD
				$return_code = $my_db->execute('INSERT leagues (`firstName`, `lastName`, `orgName`, `address`, `city`, `zipcode`, `phone`, `email`, `url`, `sport`, `ageTo`, `ageFrom`, `gender`, `eligibleZipcodes`, `registrationStart`, `registrationEnd`, `seasonStart`, `seasonEnd`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)', $data_table);
			}

			if ($return_code > 0) {
				$mess = getADMSuccess("League have been updated.");
				$url_to_email = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/add_edit_league.php?id=' . $return_code;
				sendSuccessEmail('zeeshanhashmi@gmail.com', 'zeeshanhashmi@gmail.com', 'zeeshanhashmi@gmail.com', 'LOREM IPSUM', $_SERVER['REMOTE_ADDR']);
				$data = array();
			}

		} else {
			$form_errors = $validator->getError();

			if (!empty($form_errors)) {
				foreach ($form_errors as $error) {
					$errors .= li($error);
				}
				$mess = getADMError($errors);
			}
		}
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="X-UA-Compatible" content="IE=7"/>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2"/>
	<title><?php echo $title; ?></title>

	<style type="text/css" media="all">
		@import url("css/style.css");
		@import url("css/jquery.wysiwyg.css");
		@import url("css/facebox.css");
		@import url("css/visualize.css");
		@import url("css/date_input.css");
	</style>

	<!--[if lt IE 8]>
	<style type="text/css" media="all">@import url("css/ie.css");</style><![endif]-->
	<link rel="stylesheet" href="css/jquery-ui.css" type="text/css" media="screen" />
	<script language="javascript" src="js/jquery.min.js"></script>
	<script language="javascript" src="js/jquery.ui.core.min.js"></script>
	<script language="javascript" src="js/jquery.ui.datepicker.min.js"></script>
	<script language="javascript" src="js/validate.js"></script>
	<SCRIPT language=Javascript>

	</SCRIPT>

</head>


<body>

<div id="hld">

<div class="wrapper">        <!-- wrapper begins -->

<?php include("__menu.php");?>
<!-- #header ends -->
<!-- .block ends -->
<div class="block">
<div class="block_head">
	<div class="bheadl"></div>
	<div class="bheadr"></div>
	<h2>admin setup</h2>
</div>
<!-- .block_head ends -->
<div class="block_content">
<?php echo $mess;?>

<form action="" name="f1" id="f1" method="post" class="theForm" onsubmit="return validate();">
<table width="100%" border="0" cellspacing="2" cellpadding="2">
<tr>
	<td width="38%" class="theField">First Name :</td>
	<td width="62%">
		<input name="firstName" value="<?php echo _getEcho($data,'firstName') ?>" type="text" class="text small require" id="firstName" size="30" maxlength="50"/>
	</td>
</tr>
<tr>
	<td class="theField"> Last Name :</td>
	<td>
		<input name="lastName" value="<?php echo _getEcho($data,'lastName') ?>" type="text" class="text small require" id="lastName" size="30" maxlength="50"/></td>
</tr>
<tr>
	<td class="theField">Name of league / org :</td>
	<td><input name="orgName" value="<?php echo _getEcho($data,'orgName') ?>" type="text" class="text small require" id="orgName" size="30" maxlength="50"/></td>
</tr>
<tr>
	<td class="theField"> Address :</td>
	<td><input name="address" value="<?php echo _getEcho($data,'address') ?>" type="text" class="text small require" id="address" size="30" maxlength="255"/></td>
</tr>
<tr>
	<td class="theField">City :</td>
	<td><input name="city" value="<?php echo _getEcho($data,'city')  ?>" type="text" class="text small require" id="city" size="30" maxlength="150"/></td>
</tr>
<tr>
	<td class="theField">State :</td>
	<td>
		<select name="state" class="mySelect require" id="state">
			<option value="">Choose one....</option>
			<?php
				$states = getStateName();
				foreach ($states as $key => $val):
			?>
				<option value="<?=$key;?>" <?php if (isset($data['state']) && $data['state'] == $key) echo "SELECTED" ?> ><?=$val;?></option>
			<?php endforeach;?>
		</select>
	</td>

</tr>
<tr>
	<td class="theField"> Zipcode:</td>
	<td><input name="zipcode" value="<?php echo _getEcho($data,'zipcode') ?>" type="text" class="text small require" id="zipcode" size="30" maxlength="10"
	           onkeypress="return isNumberKey(event)"/></td>
</tr>
<tr>
	<td class="theField"> Phone:</td>
	<td><input name="phone" value="<?php echo _getEcho($data,'phone') ?>" type="text" class="text small require" id="phone" size="30" maxlength="15"/></td>
</tr>
<tr>
	<td class="theField"> Email:</td>
	<td><input name="email" value="<?php echo _getEcho($data,'email') ?>" type="text" class="text small require" id="email" size="30" maxlength="50"/></td>
</tr>
<tr>
	<td class="theField"> Url:</td>
	<td><input name="url" value="<?php echo _getEcho($data,'url') ?>" type="text" class="text small require" id="url" size="30" maxlength="255"/></td>
</tr>
<tr>
	<td class="theField"> Sport:</td>
	<td>
		<select name="sport" class="theInput require" id="sport">
			<option value="">Choose one....</option>
			<?php
				foreach ($sports as $id => $name):
			?>
			<option value="<?=$id?>" <?if (isset($data['sport']) && $data['sport']== $id) echo "SELECTED"?> ><?=$name?></option>
			<?php endforeach; ?>
		</select>
	</td>
</tr>
<tr>
	<td class="theField"> Age From:</td>
	<td><input name="ageFrom" value="<?php echo _getEcho($data,'ageFrom') ?>" type="text" class="text small require" id="ageFrom" size="30" maxlength="10"
	           onkeypress="return isNumberKey(event)"/></td>
</tr>
<tr>
	<td class="theField"> Age To:</td>
	<td><input name="ageTo" value="<?php echo _getEcho($data,'ageTo') ?>" type="text" class="text small require" id="ageTo" size="30" maxlength="10"
	           onkeypress="return isNumberKey(event)"/></td>
</tr>
<tr>
	<td class="theField"> Gender:</td>
	<td>
		<select id="gender" name="gender" class="myselect require">
			<option value="">Choose...</option>
			<option value="male" <?php if (isset($data['gender']) && $data['gender'] == 'male') echo "checked = 'checked'" ?> >Male</option>
			<option value="female" <?php if (isset($data['gender']) && $data['gender'] == 'female') echo "checked = 'checked'"?> >Female</option>
			<option value="both" <?php if (isset($data['gender']) && $data['gender'] == 'both') echo "checked = 'checked'"?> >both</option>
		</select>
	</td>
</tr>
<tr>
	<td class="theField"> Eligible Zipcodes:</td>
	<td><input name="eligibleZipcodes" value="<?php echo _getEcho($data,'eligibleZipcodes') ?>" type="text" class="text small require" id="eligibleZipcodes" size="30"
	           maxlength="250"/></td>
</tr>
<tr>
	<td class="theField">Registration Start Date:</td>
	<td><input name="registrationStart" value="<?php echo _getEcho($data,'registrationStart') ?>" type="text" class="text small require" id="registrationStart" size="30"
	           maxlength="10"/></td>
</tr>
<tr>
	<td class="theField">Registration End Date:</td>
	<td><input name="registrationEnd" value="<?php echo _getEcho($data,'registrationEnd') ?>" type="text" class="text small require" id="registrationEnd" size="30"
	           maxlength="10"/></td>
</tr>
<tr>
	<td class="theField">Season Start Date:</td>
	<td><input name="seasonStart" value="<?php echo _getEcho($data,'seasonStart') ?>" type="text" class="text small require" id="seasonStart" size="30" maxlength="30"/>
	</td>
</tr>
<tr>
	<td class="theField">Season End Date:</td>
	<td><input name="seasonEnd" value="<?php echo _getEcho($data,'seasonEnd') ?>" type="text" class="text small require" id="seasonEnd" size="30" maxlength="30"/></td>
</tr>
<tr>
	<td></td>
	<td>
		<input name="Submit" type="submit" class="submit mid" id="Submit" value="Save"/>
		<input name="Reset" type="reset" class="submit mid" value=" Reset "/>
	</td>
</tr>
</table>

</form>
</p>
</div>
<!-- .block_content ends -->
</div>
<!-- .block ends -->
<!-- .block ends -->
<!-- .block.small.left ends -->
<!-- .block.small.right ends -->
<!-- .block ends -->
<!-- .block ends -->
<!-- .leftcol ends -->
<?php include("__footer.php"); ?>
</div>
<!-- wrapper ends -->

</div>
<!-- #hld ends -->
</body>
</html>